#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+5;
inline int read(){
	int x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-48,ch=getchar();}
	return x*f;
}
int n,q,siz[N],p[N<<2],tot,p2[N<<2],cnt,t[N];
vector<int> e[N];
signed main(){
	freopen("dfs.in","r",stdin);
	freopen("dfs.out","w",stdout);
	n=read(),q=read();
	for(int i=1;i<=n;i++){
		siz[i]=read();
		for(int x,j=1;j<=siz[i];j++){
			x=read();
			e[i].emplace_back(x);
		}
	}
	int u=1;
	while(u!=n){
		t[u]++;
		t[u]%=siz[u];
		u=e[u][t[u]];
		p[++tot]=u;
	}
	for(int i=n-1;i>=1;i--) p2[++cnt]=i;
	for(int i=2;i<=n;i++) p2[++cnt]=i;
	while(q--){
		int k=read();
		if(k<=tot) printf("%lld\n",p[k]);
		else{
			k-=tot;
			k%=cnt;
			if(!k) k=cnt;
			printf("%lld\n",p2[k]);
		}
	}
	return 0;
}